Overview

In previous exercises, you have already set up a LittleLemon project that includes the Restaurant app. You have also declared the Menu and Booking models, migrated to the MySQL database.

In this exercise, you will build a REST API to perform CREATE, READ, UPDATE AND DELETE operations on the Menu model

Scenario

You will use Django REST Framework to build the APIs in the restaurant app. You will also create the views using DRF's generic views.

Step 1

Install the 'rest_framework' with PIP utility while remaining in the Django virtual environment directory.

Open the command prompt in the LittleLemon directory and start VS Code.

Step 2

Open the settings.py file and add 'rest_framework' in the LittleLemon project's INSTALLED_APPS list.

Step 3

The Serializer in Django REST Framework converts compound data types into JSON or XML format. ModelSerializer is a special type of serializer that quickly creates a serializer class from the Django model fields.

The ModelSerializer class is declared in the rest_framework.serializers module.

In the app package folder, create a new file called serializers.py and declare the MenuSerializer class that subclasses ModelSerializer .

Step 4

Open the views.py module in the restaurant app package folder.

Declare two classes:

  1. MenuItemView – inheriting the rest_framework.generics.ListCreateView class. It handles the POST and GET method calls.

  2. SingleMenuItemView – inherits the RetrieveUpdateAPIView and DestroyAPIView classes both imported from the rest_framework.generics module. This class is responsible for processing GET, PUT and DELETE method calls.

Step 5

Define URL routes for the restaurant app's menu views in the urls.py file.

#add following lines to urlpatterns list 
path('menu/', views.MenuItemsView.as_view()),
path('menu/<int:pk>', views.SingleMenuItemView.as_view()),

Include app URLs in the LittleLemon project's urls.py module.

#add following line to urlpatterns list 
path('restaurant/menu/',include(restaurant.urls'))  

Step 6

Run Django server, and open the URL http://localhost:8000/restaurant/menu/items .

This is the URL of the browsable API. Perform POST operations to add menu items. With the GET request, fetch the list of items.

Tips

Use the following code snippet to declare the serializer class:

#define Serializer class for User model
from django.contrib.auth.models import User 
from rest_framework import serializers 

class UserSerializer(serializers.ModelSerializer): 

    class Meta: 
        model = User 
        fields = ['url', 'username', 'email', 'groups']

You can use the following code snippet to define views:

from rest_framework.decorators import api_view
from .models import MenuItem
from .serializers import MenuItemSerializer

# Create your views here. 
class MenuItemsView(generics.ListCreateAPIView): 
queryset = MenuItem.objects.all() 
serializer_class = MenuItemSerializer

class SingleMenuItemView(generics.RetrieveUpdateAPIView, generics.DestroyAPIView):     
queryset = MenuItem.objects.all() 
serializer_class = MenuItemSerializer

Conclusion

In this exercise, you have used Django REST framework to set up the Menu API for the Restaurant app.